package s2.s2_3;

import java.io.*;
import java.util.BitSet;

/*
ID: huangmingbo
LANG: JAVA
TASK: prefix
 */
public class prefix {

    private static PreNode HEAD = new PreNode();

    public static void main(String[] args) throws IOException {


        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(prefix.class.getResourceAsStream(prefix.class.getSimpleName() + ".in")));
                PrintWriter writer = new PrintWriter(new FileWriter(prefix.class.getSimpleName() + ".out"));
        ) {
            String tmp;
            String[] tmpArr;
            PreNode tmpNode = HEAD;
            while (!".".equals(tmp = reader.readLine())) {
                tmpArr = tmp.split(" ");
                for (String s : tmpArr) {
                    tmpNode.next = new PreNode(s);
                    tmpNode = tmpNode.next;
                }
            }


            StringBuilder sb = new StringBuilder();
            while ((tmp = reader.readLine()) != null) {
                sb.append(tmp);
            }

            int count = search(sb.toString());
            writer.println(count);
        }
    }

    private static int search(String string) {
        BitSet bitSet = new BitSet(string.length());
        int maxIdx = -1;

        for (int i = 0; i < string.length() && i-maxIdx <= 10; i++) {
            if (search(string, i, bitSet)) {
                maxIdx = i;
            }
        }

        return maxIdx >= 0 ? maxIdx + 1 : 0;
    }

    private static boolean search(String string, int idx, BitSet bitSet) {

        PreNode tmp = HEAD;
        int tmpLen;
        while ((tmp = tmp.next) != null) {
            if (idx + 1 < (tmpLen = tmp.pre.length())) {
                continue;
            }

            if (idx + 1 == tmpLen) {
                if (string.substring(0, idx + 1).equals(tmp.pre)) {
                    bitSet.set(idx);
                    return true;
                }

                continue;
            }

            if (bitSet.get(idx - tmpLen) && string.substring(idx - tmpLen + 1, idx + 1).equals(tmp.pre)) {
                bitSet.set(idx);
                return true;
            }

        }

        return false;
    }

    private static class PreNode {
        String pre;
        PreNode next;

        PreNode(String pre) {
            this.pre = pre;
        }

        PreNode() {
        }
    }
}
